package com.zsc.controller.user;

import com.zsc.dto.OrdersPageQueryDTO;
import com.zsc.result.PageResult;
import com.zsc.result.Result;
import com.zsc.service.OrderDetailService;
import com.zsc.service.OrdersService;
import com.zsc.vo.OrderDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Qiuxiaoqiang
 * @Date 2024/6/21 15:09
 * @Version 1.0
 */

@RestController("userOrderController")
@Api(tags = "用户端 订单管理接口")
@Slf4j
@RequestMapping("/user/order")
public class OrderController {
    @Autowired
    OrdersService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 分页条件查询订单
     *
     * @param ordersPageQueryDTO
     * @return
     */

    @GetMapping("/conditionSearch")
    @ApiOperation("分页查询订单")
    public Result<PageResult> page(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("分页查询订单，参数：{}", ordersPageQueryDTO);
        PageResult pageResult = orderService.historyOrderPageQuery(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("orderDetail/{id}")
    @ApiOperation("订单详情")
    public Result<OrderDetailVO> detail(@PathVariable Long id) {
        log.info("查询订单详情，订单id：{}", id);
        OrderDetailVO orderHistoryVO = orderService.OrderDetail(id);
        return Result.success(orderHistoryVO);
    }


    /**
     * 再来一单
     *
     * @param id
     * @return
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result repetitionOrder(@PathVariable Long id) {
        log.info("再来一单，订单id：{}", id);
        orderService.repeatOrder(id);
        return Result.success();
    }

    @DeleteMapping("/deleteOrder/{id}")
    @ApiOperation("取消订单")
    @Transactional
    public Result deleteOrder(@PathVariable Long id) {
        log.info("取消订单，订单id:{}", id);
        orderService.removeById(id);
        orderDetailService.removeByOrderId(id);
        return Result.success();
    }
}
